package com.egdigital.allzic.ui.adapter;

import java.util.List;

import android.content.Context;
import android.text.format.DateUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.daimajia.androidanimations.library.Techniques;
import com.daimajia.androidanimations.library.YoYo;
import com.egdigital.allzic.R;
import com.egdigital.allzic.data.object.Station;
import com.egdigital.allzic.utils.ImageManager;
import com.egdigital.allzic.utils.Utils;

public class AdapterListRadio extends BaseAdapter {

	private static final String	TAG	= AdapterListRadio.class.getSimpleName();	// log
	private Context				mContext;
	private List<Station>		mList;
	private LayoutInflater		mInflater;
	private ImageManager		mImageManager;
	private int					mCellId;

	private static class HolderWebRadio {

		TextView		mTitle;
		TextView		mNumber;
		TextView		mTime;
		ImageView		mImage;
		RelativeLayout	mLayout;
	}

	public AdapterListRadio(Context context, List<Station> list, int cellId) {
		mContext = context;
		mList = list;
		mCellId = cellId;
		mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mImageManager = ImageManager.getInstance(context);
		mImageManager.setOptions();
		mImageManager.setImageLoader();
	}

	public void setData(List<Station> data) {
		mList = data;
	}

	public int getCount() {
		return mList.size();
	}

	public String getItem(int position) {
		return mList.get(position).getmTitle();
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		HolderWebRadio holder = null;
		if (convertView == null) {
			convertView = mInflater.inflate(mCellId, parent, false);
			holder = new HolderWebRadio();
			// get
			switch (mCellId) {
				case R.layout.cell_top10:
					holder.mTitle = (TextView) convertView.findViewById(R.id.adapter_top10_title);
					holder.mImage = (ImageView) convertView.findViewById(R.id.adapter_top10_thumb);
					holder.mLayout = (RelativeLayout) convertView.findViewById(R.id.adapter_top10_layout);
					holder.mNumber = (TextView) convertView.findViewById(R.id.adapter_top10_number);
					holder.mNumber.setVisibility(View.VISIBLE);
					break;
				case R.layout.cell_list_recent:
					holder.mTitle = (TextView) convertView.findViewById(R.id.cell_recent_title);
					holder.mImage = (ImageView) convertView.findViewById(R.id.cell_recent_thumb);
					holder.mLayout = (RelativeLayout) convertView.findViewById(R.id.cell_recent_layout);
					holder.mTime = (TextView) convertView.findViewById(R.id.cell_recent_sub);
					break;
				default:
					holder.mTitle = (TextView) convertView.findViewById(R.id.adapter_webradio_title);
					holder.mImage = (ImageView) convertView.findViewById(R.id.adapter_webradio_thumb);
					holder.mLayout = (RelativeLayout) convertView.findViewById(R.id.adapter_webradio_layout);
					break;
			}
			YoYo.with(Techniques.FadeIn).duration(500).playOn(convertView);
			convertView.setTag(holder);
		} else {
			holder = (HolderWebRadio) convertView.getTag();
		}
		// color
		if (position % 2 == 0) {
			holder.mLayout.setBackgroundResource(R.drawable.selector_list_blue);
		} else {
			holder.mLayout.setBackgroundResource(R.drawable.selector_list_simple);
		}
		switch (mCellId) {
			case R.layout.cell_top10:
				holder.mNumber.setVisibility(View.VISIBLE);
				holder.mNumber.setText(position + 1 + "");
				try {
					Utils.setImageFromUrl(mImageManager, mList.get(position).getmThumb(), holder.mImage);
				} catch (Exception e) {
				}
				break;
			case R.layout.cell_list_recent:
				holder.mTime.setText(DateUtils.getRelativeDateTimeString(mContext, Long.parseLong(mList.get(position).getmDate()), DateUtils.MINUTE_IN_MILLIS, DateUtils.YEAR_IN_MILLIS, 0));
				try {
					Utils.setImageFromUrl(mImageManager, mList.get(position).getmThumb(), holder.mImage);
				} catch (Exception e) {
				}
				break;
			case R.layout.cell_list_radio:
				try {
					Utils.setImageFromUrl(mImageManager, mList.get(position).getmThumb(), holder.mImage);
				} catch (Exception e) {
				}
				break;
		}
		// set
		holder.mTitle.setText(getItem(position));
		return convertView;
	}
}
